
import { getUserList , insertOneUser, deleteUserByIds ,updateOneUser }  from "../services/user/userService";

export default {
  namespace: 'user',
  state: {
    list:[],
    total:0,
    pageIndex:1,
    pageSize:5,
    userItem:{},
    updateVisible:false,
    viewVisible:false,
    searchParams:{},
    selectedRowKeys:[],
    selectedRows:[]
  },
  effects: {
    *getUserList({payload:{pageIndex = 1,pageSize,searchParams = {}}},{ call,put }) {
      const data = yield call(getUserList,{pageIndex,pageSize,params:searchParams});
      if(data.code === '200'){
        console.log("model层effects执行state更新");
        yield put({
            type:"updateState",
            payload:{
                pageIndex:data.current||1,
                list:data.data ||[],
                total:data.total
            }
        });
        console.log("当前的状态：",this);
      }
    },
    *insertUser({payload:{values}},{call,put}){
      const data =yield call(insertOneUser ,values);
      if(data.code === '200'){
        console.log("插入用户成功，即将关闭模态");
        console.log("去更新下state");
        yield put({
          type:"updateState",
          payload:{
            visible:false
          }
      });
      yield put({type:"getUserList",payload:{}});;
      }
    },

    *deletetUsers({payload:{ids}},{call,put}){
      const formData = new FormData();
      formData.append("ids", ids);
      const data =yield call(deleteUserByIds,formData);
      if(data.code === '200'){
        console.log("删除用户成功");
        console.log("去更新下state");
        yield put({
          type:"updateState",
          payload:{
            selectedRows:[],
            selectedRowKeys:[],
          }
      });
      yield put({type:"getUserList",payload:{}});
      }
    },

    *updateUser({payload:{values}},{call,put}){
      const data =yield call(updateOneUser ,values);
      if(data.code === '200'){
        console.log("修改用户成功，即将关闭模态");
        console.log("去更新下state");
        yield put({
          type:"updateState",
          payload:{
            updateVisible:false,
          }
      });
      yield put({type:"getUserList",payload:{}});
      }
    },
  },
  reducers: {
    updateState(state, { payload }) {
      console.log("又来更新state了");
      console.log("新state为:", ...state, ...payload );
      return { ...state, ...payload };
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
        return history.listen(({ pathname }) => {
            if (pathname === '/user') {
                dispatch({type:'getUserList',payload:{ }});
                //axios.get("http://10.0.10.220/FineReport/ReportServer/FineReport/ReportServer?reportlet=jsbim-reportlets/控制点清单.cpt&token=eyJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50SWQiOiIzMGUyMWEwZDQ1MzU0MGIyODlkNGE2YmRiZTNhZjZhZiIsIm5hbWUiOiJMTF_mlr3lt6UwMDEiLCJ1c2VybmFtZSI6ImxsX1NoaUdvbmcwMDEiLCJwaG9uZU51bWJlciI6IjE1MjA5MjUwMDAxIiwiYWNjb3VudFR5cGUiOiJQRVJTT05BTCIsInVzZXJJZCI6IjU1NDUyNjE3ZDY2NjRhZGRhMjVlZmM1YjExNWM1MWM4IiwiY29tcGFueUlkIjoiZjUyYmM3MmNjZWE2NDdlMDkzMzRiNjJmMzhhYzZiOWIiLCJjb21wYW55TmFtZSI6IkxM5pa95bel5Y2V5L2NMigwNi4xNuWPmOabtOabtCkiLCJqdGktdXVpZCI6Imp0aS0zNDRhZjZmYS00YWQ5LTQxNzgtYWQ3OC1hYjM1YTIzZWYyYWIiLCJleHAiOjE2NTU0ODc3ODB9.A0PEeSLY3i20if7TiGWtviFzi-98tzbqBo-7Q2pEi8k&apiDomain=http://jspro-gateway-test.gcnao.cn/sz-qualityapp-service&controlId=035a2e18ac1049afb863e0f57b9d2a00&format=excel&random=0.03814574282578542"); 
              }
        });
    },
  },
};

